# -*- coding : utf-8 -*-
# author : Big Cat
# time : 2021/7/20:22:09
# file : basepage.py
import os
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from comm import dir_config as Dir
from comm.logger import FrameLog as log

class BasePage:

    def __init__(self,page_name=__file__,driver = webdriver.Chrome()):
        self.logger = log().getLogger()
        self.driver = driver
        self.page_name = page_name

    def locator(self,loc):
        try:
            el = self.driver.find_element(*loc)
            self.logger.info(f"在{self.page_name}页面,定位到元素：{loc}")


        except:
            self.logger.error(f"在{self.page_name}页面,未定位到元素：{loc}")
            raise
        return el

    # 等待元素可见
    def wait_ele_visibility(self,loc,timeout=15, poll_fre=0.5):
        try:
            WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll_fre).until(
                EC.visibility_of_element_located(loc))
            self.logger.info(f"在{self.page_name}页面,等待元素可见：{loc}")
        except:
            self.logger.error(f"在{self.page_name}页面,未等待到元素可见：{loc}")




    #等待元素存在
    def wait_ele_presence(self,loc,timeout=15, poll_fre=0.5):
        try:
            WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll_fre).until(
                EC.presence_of_element_located(loc))

            self.logger.info(f"在{self.page_name}页面,等待元素存在：{loc}")
        except:
            self.logger.error(f"在{self.page_name}页面,未等待到元素：{loc}")



    def input(self,loc,value):
        try:
            self.wait_ele_visibility(loc)
            self.locator(loc).send_keys(value)
            self.logger.info(f"在{self.page_name}页面，输入信息：{value}")
        except:
            self.save_page_shoot()
            self.logger.error(f"在{self.page_name}页面，未成功输入信息：{value}")



    def click(self,loc):
        try:
            self.wait_ele_visibility(loc)
            self.locator(loc).click()
            self.logger.info(f"在{self.page_name}页面,点击元素：{loc}")
        except:
            self.save_page_shoot()
            self.logger.error(f"在{self.page_name}页面,未成功点击到元素：{loc}")





    def mouse_move(self,loc):
        try:
            self.wait_ele_visibility(loc)
            ActionChains(self.driver).move_to_element(self.locator(loc)).perform()
            self.logger.info(f"在{self.page_name}页面,鼠标移动到元素：{loc}")
        except:
            self.logger.error(f"在{self.page_name}页面,鼠标未成功移动到元素：{loc}")
            self.save_page_shoot()



    def sleep(self,s):
        time.sleep(s)

    def run(self,function_name,*args):
        getattr(self,function_name)(*args)


    def save_page_shoot(self):
        filename = os.path.join(Dir.img_dir,self.page_name+f'{time.strftime("%Y%m%d%H%M%S",time.localtime())}'+'.png')
        self.driver.save_screenshot(filename)
        self.logger.info(f"失败截图，截取页面:{self.page_name}，存储的路径：{filename}")

if __name__ == '__main__':
    driver = BasePage()
    driver.run('click','chuiziye','ceshi ')